/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   main.cpp
 * Author: blickensdorf
 *
 * Created on 04 February 2019, 14:09
 */
#include <cstdlib>


#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#include <boost/algorithm/string/replace.hpp>
#include <sstream>


using namespace std;
#define bufSize 1024
/*
 * 
 */
int main(int argc, char** argv) {
    ofstream myfile;

    if (argc < 3) {
        cout << "Error: Missing input/output path\n";
        return -1;
    } else if (argc > 3) {
        cout << "Error: Too many arguments\n";
        return -1;
    } else {
        string input, output;
        std::istringstream inbuf1(argv[1]);
        inbuf1 >> input;
        std::istringstream inbuf2(argv[2]);
        inbuf2 >> output;

        std::ifstream infile(input);        //enter input wrl file here
        myfile.open(output);           //enter output file in here
        std::string line;
        std::string ID;

        if (!infile) {
            cout << "Error: Can not find file " << input << "\n";
            return -1;
        }

        while (std::getline(infile, line)) {
            if (line.find("FilamentSegment") != string::npos) {
                //i found the line of a segment, and now i need to extract the number
                boost::replace_all(line, "                    DEF FilamentSegment", "");
                boost::replace_all(line, "     Group {", "");
//            cout << line << endl;
                boost::replace_all(line, " ", "");
//            cout << line << endl;
                ID = line;
            }
            if (line.find("point") != string::npos) {
//            myfile << line  <<  std::endl;           //print each line
                //read further until we find a closing bracket
                size_t foundBracket = line.find(']');
                while (foundBracket == string::npos) {

                    //only save line if number if spaces does fit X
                    int counter = 0;
                    for (int i = 0; i < line.size(); i++) {
                        if (line.at(i) == ' ')counter++;
                    }
                    if (counter == 40) {  // to match the right lines
                        // replace all ,
                        std::replace(line.begin(), line.end(), ',', ' '); // replace all ',' to ' '
                        //remove the overhead
                        boost::replace_all(line, "                                      ", " ");
                        boost::replace_all(line, "\n", "");
                        myfile << ID.substr(0, 12) << " " << line << std::endl;           //print each line
//                    cout << ID << " " << line <<  std::endl;           //print each line
                    }
                    std::getline(infile, line);
                    foundBracket = line.find(']');
//                cout << foundBracket << endl;
                }
//            cout << "stopping to read" << endl;
            }
        }
    }
    
    return 0;
}

